package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.LPaymethod;
import com.ruoyi.system.template.LGuestTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LAccount;
import com.ruoyi.system.service.ILAccountService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 账户Controller
 *
 * @author zyb
 * @date 2022-01-02
 */
@RestController
@RequestMapping("/system/account")
public class LAccountController extends BaseController
{
    @Autowired
    private ILAccountService lAccountService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询账户列表
     */
    @PreAuthorize("@ss.hasPermi('system:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(LAccount lAccount)
    {
        startPage();
        List<LAccount> list = lAccountService.selectLAccountList(lAccount);
        return getDataTable(list);
    }

    /**
     * 导出账户列表
     */
    @PreAuthorize("@ss.hasPermi('system:account:export')")
    @Log(title = "账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LAccount lAccount)
    {
        List<LAccount> list = lAccountService.selectLAccountList(lAccount);
        ExcelUtil<LAccount> util = new ExcelUtil<LAccount>(LAccount.class);
        util.exportExcel(response, list, "账户数据");
    }

    /**
     * 获取账户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:account:query')")
    @GetMapping(value = "/{accid}")
    public AjaxResult getInfo(@PathVariable("accid") Long accid)
    {
        return AjaxResult.success(lAccountService.selectLAccountByAccid(accid));
    }

    /**
     * 新增账户
     */
    @PreAuthorize("@ss.hasPermi('system:account:add')")
    @Log(title = "账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LAccount lAccount)
    {
        redisCache.deleteObject("account");
        return toAjax(lAccountService.insertLAccount(lAccount));
    }

    /**
     * 修改账户
     */
    @PreAuthorize("@ss.hasPermi('system:account:edit')")
    @Log(title = "账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LAccount lAccount)
    {
        redisCache.deleteObject("account");
        return toAjax(lAccountService.updateLAccount(lAccount));
    }

    /**
     * 删除账户
     */
    @PreAuthorize("@ss.hasPermi('system:account:remove')")
    @Log(title = "账户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{accids}")
    public AjaxResult remove(@PathVariable Long[] accids)
    {
        redisCache.deleteObject("account");
        return toAjax(lAccountService.deleteLAccountByAccids(accids));
    }

    @GetMapping("/list/all")
    public AjaxResult listAll()
    {
        List<LAccount> list = redisCache.getCacheList("account");
        if(list.size()==0){
            LAccount account= new LAccount();
            list = lAccountService.selectLAccountList(account);
            redisCache.setCacheList("account",list);
        }
        return AjaxResult.success(list);
    }


    @Log(title = "客户导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:account:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LAccount> util = new ExcelUtil<LAccount>(LAccount.class);
        List<LAccount> lAccountList = util.importExcel(file.getInputStream());
        String message = lAccountService.importAccount(lAccountList, updateSupport);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<LAccount> util = new ExcelUtil<LAccount>(LAccount.class);
        util.importTemplateExcel(response, "客户数据");
    }
}
